package com.gc.controller.courseServlet;


import com.gc.entity.Course;
import com.gc.entity.Student;
import com.gc.entity.Teacher;
import com.gc.repository.CourseRepository;
import com.gc.repository.TeacherRepository;
import com.gc.repository.impl.CourseRepositoryImpl;
import com.gc.repository.impl.TeacherRepositoryImpl;
import com.gc.util.result.R;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "courseAdd", value = "/courseAdd")
public class courseAdd extends HttpServlet {
    CourseRepository courseRepository = new CourseRepositoryImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Gson gson = new Gson();
        boolean res = courseRepository.Add(new Course(
                null,
                request.getParameter("name"),
                request.getParameter("cid")));

        if(res) {
            response.getWriter().write(gson.toJson(R.ok().data("ok")));
        } else {
            response.getWriter().write(gson.toJson(R.error()));
        }

    }
}
